<?php

declare(strict_types=1);

namespace App\CustHub\Controller;

use App\CustHub\Annotation\ApiAuth;
use App\CustHub\Annotation\ChOperationLog;
use App\CustHub\Annotation\ChPermission;
use App\CustHub\Annotation\VerifySignature;
use App\CustHub\Dto\RobotExcelDto;
use App\CustHub\Request\RobotRequest;
use App\CustHub\Service\ChRobotService;
use App\CustHub\Service\RobotLoginService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Mine\MineController;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;

#[Controller(prefix: 'ch/robot'),ApiAuth,VerifySignature]
class RobotController extends BaseController
{
    public array $noNeedLogin = ['export','resetClient'];
    public array $noNeedVerifySign = ['export'];

    #[Inject]
    protected ChRobotService $service;

    #[Inject]
    protected RobotLoginService $loginService;

    /**
     * 机器人列表
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('list'),ChPermission("ch:robot:list")]
    public function list(): ResponseInterface
    {
        return $this->success($this->service->list($this->request->all()));
    }

    /**
     * 删除机器人
     * @param RobotRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('delete'),ChOperationLog]
    public function delete(RobotRequest $request): ResponseInterface
    {
        return $this->service->deleteById($request->input('id')) ? $this->success() : $this->error();
    }

    /**
     * 自动续费开关
     * @param RobotRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('autoSwitch'),ChOperationLog]
    public function autoSwitch(RobotRequest $request): ResponseInterface
    {
        return $this->service->changeStatus($request->input('id'),$request->input('is_auto_renewal'),'is_auto_renewal') ? $this->success() : $this->error();
    }

    /**
     * 登录二维码
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('loginQrCode')]
    public function loginQrCode(): ResponseInterface
    {
        $data = $this->loginService->getLoginQrCode($this->request->all());
        return $this->success($data);
    }

    /**
     * 验证登录
     * @param RobotRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \RedisException
     */
    #[PostMapping('verifyLogin')]
    public function verifyLogin(RobotRequest $request): ResponseInterface
    {
        $data = $this->loginService->verifyLogin($request->all());
        return $this->success($data);
    }

    /**
     * 机器人登录
     * @param RobotRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \RedisException
     */
    #[PostMapping('robotLogin')]
    public function robotLogin(RobotRequest $request): ResponseInterface
    {
        $data = $this->loginService->robotLogin($request->all());
        return $this->success(['status'=>$data]);
    }

    /**
     * 自动登录
     * @param RobotRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('autoLogin')]
    public function autoLogin(RobotRequest $request): ResponseInterface
    {
        return $this->loginService->autoLogin($request->all())? $this->success() : $this->error();
    }

    /**
     * 二次登录状态校验
     * @param RobotRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('checkLogin')]
    public function checkLogin(RobotRequest $request): ResponseInterface
    {
        return $this->success(['status'=>$this->service->checkLogin($request->all())]);
    }

    /**
     * 注销登录
     * @param RobotRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('loginOut')]
    public function loginOut(RobotRequest $request): ResponseInterface
    {
        $this->loginService->loginOut($request->all());
        return $this->success();
    }

    #[PostMapping('getLabelList')]
    public function getLabelList(): ResponseInterface
    {
        $data = $this->service->getLabelList($this->request->all());
        return $this->success($data);
    }

    #[PostMapping('selectList')]
    public function selectList(): ResponseInterface
    {
        $data = $this->service->getRobotSelectList($this->request->all());
        return $this->success($data);
    }

    #[PostMapping('loginRecord')]
    public function loginRecord(RobotRequest $request)
    {
        return $this->success($this->service->loginRecord($request->all()));
    }

    #[GetMapping('export')]
    public function export()
    {
        return $this->service->export($this->request->all(),RobotExcelDto::class);
    }

    /**
     * 获取个人二维码
     * @param RobotRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('getPersonalQRCode')]
    public function getPersonalQRCode(RobotRequest $request): ResponseInterface
    {
        return $this->success(['qr_code'=>$this->service->getPersonalQRCode($request->all())]);
    }

    /**
     * 修改备注
     * @param RobotRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('editRemark')]
    public function editRemark(RobotRequest $request): ResponseInterface
    {
        return $this->service->editRemark($request->all()) ? $this->success() : $this->error();
    }

    /**
     * 重置客户端
     * @param RobotRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('resetClient')]
    public function resetClient(RobotRequest $request): ResponseInterface
    {
        return $this->service->resetClient($request->all()) ? $this->success() : $this->error();
    }

    /**
     * 更新机器人先关信息
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('updateRobot')]
    public function updateRobot(): ResponseInterface
    {
        $this->service->updateRobot($this->request->all());
        return $this->success('操作成功，请稍后刷新页面查看结果~');
    }
}